package com.hwg.emp.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.common.dto.KeysQueryDto;
import com.hwg.emp.common.util.ThisAppUtil;
import com.hwg.emp.model.AttendanceMD;
import com.hwg.emp.model.OvertimeMD;
import com.hwg.emp.service.AttendanceService;
import com.hwg.emp.service.ServiceFactroy;

public class selectOwnAttendanceController extends HttpServlet{
	AttendanceService attendanceservice=ServiceFactroy.getService(AttendanceService.class);
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String askoffdaysStr=req.getParameter("days");
		Integer askoffdays=null;
		if (!ThisAppUtil.isNone(askoffdaysStr)) {
			askoffdays =Integer.parseInt(askoffdaysStr);
		}
	/*	String startdateStr=req.getParameter("startdate");
		Date startdate=null;
		if (ThisAppUtil.isNone(startdateStr)) {
			startdate=ThisAppUtil.parseDate(startdateStr, "yyyy-MM-dd");
		}
		Date startdate	=(ThisAppUtil.parseDate(req.getParameter("startdate"),"yyyy-MM-dd"));
*/		
		
		//map.put("startdate", startdate);
		String pageNoStr=req.getParameter("page");
		if (pageNoStr==null) {
			pageNoStr="1";
		}
		Integer pageNo=null;
		if (!ThisAppUtil.isNone(pageNoStr)) {
			pageNo=Integer.parseInt(pageNoStr);
		}
		String pageSizeStr=req.getParameter("pageSize");
		Integer pageSize=null;
		if (pageSizeStr==null) {
			pageSizeStr="4";
		}
		if (!ThisAppUtil.isNone(pageSizeStr)) {
			pageSize=Integer.parseInt(pageSizeStr);
		}
		Map<String, Object> map=new HashMap<>();
		map.put("askoffdays", askoffdays);
		KeysQueryDto keys= new KeysQueryDto();
		keys.setKeys(map);
		keys.setPageNo(pageNo);
		keys.setPageSize(pageSize);
		List<AttendanceMD> attendanceMDs=new ArrayList<>();
		int total = 0;
		try {
			//总记录数
			total = attendanceservice.selectAttendanceByKey(keys, attendanceMDs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		req.setAttribute("attendanceMDs", attendanceMDs);
		req.setAttribute("total", total);
		req.setAttribute("pageNo", pageNo);
		req.setAttribute("pageSize", pageSize);
		//起始记录数
		int totalNo=(pageNo-1)*pageSize;
		req.setAttribute("pageList", getPageList(total, pageNo, pageSize));
		req.getRequestDispatcher("/pages/selectOwnAttendance.jsp").forward(req, resp);
		
	}
	private String getPageList(int total,int pageNo,int pageSize){
		//算出多少页
		int totalPage = total%pageSize == 0?total/pageSize:total/pageSize+1;
		StringBuffer str=new StringBuffer();
		//首页
		str.append("<li><a href='selectOwnAttendance.do?page=1'>首页</a></li>");
		//上一页
		if (pageNo==1) {
			str.append("<li class='disabled'><a>上一页</a></li>");
		} else {
			str.append("<li><a href='selectOwnAttendance.do?page="+(pageNo-1)+"'>上一页</a></li>");
		}
		//循环的页码
		for (int i = pageNo-2; i <=pageNo+2; i++) {
			if (i<1||i>totalPage) {
				continue;
			}
			if (i==pageNo) {
				str.append("<li class='active'><a>"+i+"</a></li>");
			}else{
				str.append("<li><a href='selectOwnAttendance.do?page="+i+"'>"+i+"</a></li>");
			}
		}
		//下一页
		if (pageNo==totalPage) {
			str.append("<li class='disabled'><a>下一页</a></li>");
		} else {
			str.append("<li><a href='selectOwnAttendance.do?page="+(pageNo+1)+"'>下一页</a></li>");
			
		}
		//尾页
		str.append("<li><a href='selectOwnAttendance.do?page="+totalPage+"'>尾页</a></li>");
		return str.toString();
	}

		
		
		
	}

